package com.oocl.euc.core.requestform.model;

import com.oocl.euc.audit.AuditEntity;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "PCRS_REQUEST_POSTMORTEM")
@SequenceGenerator(name = "SEQUENCE_GENERATOR", sequenceName = "REQUEST_POSTMORTEM_SEQUENCE", allocationSize = 1)
public class RequestPostmortem extends AuditEntity {


    @Column(name = "ATTENDEE")
    private String attendee;
    @Column(name = "PROBLEM")
    private String problem;
    @Column(name = "ROOT_CAUSE")
    private String rootCause;
    @Column(name = "CORRECT_ACTION")
    private String correctAction;
    @Column(name = "PREVENTIVE_ACTION")
    private String preventiveAction;
    @Column(name = "REF_NO")
    private String refNo;
    @Column(name="BUSINESS_IMPACT")
    private String businessImpact;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "REQUEST_POSTMORTEM_ID")
    private List<RequestPostmortemAttach> postmortemAttach;

    public String getAttendee() {
        return attendee;
    }

    public void setAttendee(String attendee) {
        this.attendee = attendee;
    }

    public String getProblem() {
        return problem;
    }

    public void setProblem(String problem) {
        if (problem == null) {
            this.problem = problem;
        } else {
            this.problem = problem.trim();
        }
    }

    public String getRootCause() {
        return rootCause;
    }

    public void setRootCause(String rootCause) {
        if (rootCause == null) {
            this.rootCause = rootCause;
        } else {
            this.rootCause = rootCause.trim();
        }
    }

    public String getCorrectAction() {
        return correctAction;
    }

    public void setCorrectAction(String correctAction) {
        if (correctAction == null) {
            this.correctAction = correctAction;
        } else {
            this.correctAction = correctAction.trim();
        }
    }

    public String getPreventiveAction() {
        return preventiveAction;
    }

    public void setPreventiveAction(String preventiveAction) {
        if (preventiveAction == null) {
            this.preventiveAction = preventiveAction;
        } else {
            this.preventiveAction = preventiveAction.trim();
        }
    }

    public String getRefNo() {
        return refNo;
    }

    public void setRefNo(String refNo) {
        this.refNo = refNo;
    }

    public List<RequestPostmortemAttach> getPostmortemAttach() {
        return postmortemAttach;
    }

    public void setPostmortemAttach(List<RequestPostmortemAttach> postmortemAttach) {
        this.postmortemAttach = postmortemAttach;
    }

    @Transient
    public List<RequestPostmortemAttach> getActivePostmortemAttach() {
        List<RequestPostmortemAttach> list = new ArrayList<>();
        if (this.postmortemAttach != null) {
            for (RequestPostmortemAttach attach : postmortemAttach) {
                if (attach.getStatus() == null || attach.getStatus() == 0) {

                } else {
                    list.add(attach);
                }
            }
        }
        return list;
    }

    public void addRequestPostmortemAttach(RequestPostmortemAttach attachment) {
        if (this.postmortemAttach == null) {
            this.postmortemAttach = new ArrayList<>();
        }
        if (!this.postmortemAttach.contains(attachment)) {
            attachment.setStatus(1);
            this.postmortemAttach.add(attachment);
        }
    }

    public void removeRequestPostmortemAttach(int index) {
        if (this.getActivePostmortemAttach() != null) {
            RequestPostmortemAttach attach = this.getActivePostmortemAttach().get(index);
            attach.setStatus(0);
        }
    }

    public String getBusinessImpact() {
        return businessImpact;
    }

    public void setBusinessImpact(String businessImpact) {
        this.businessImpact = businessImpact;
    }
}
    